﻿namespace PlugNT.Pack.Editer
{
    using System;
    using System.ComponentModel;
    using System.Security.Permissions;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    [ToolboxData("<{0}:EditerBox runat=server></{0}:EditerBox>"), DefaultEvent("UploadChanged"), DefaultProperty("RootPath"), AspNetHostingPermission(SecurityAction.Demand, Level=AspNetHostingPermissionLevel.Minimal)]
    public class EditBox : Panel, INamingContainer
    {
        public EditType EType = EditType.Msn;

        private string GetText()
        {
            return HttpContext.Current.Request.Form["h" + this.ID];
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            EventArgs args = new EventArgs();
            base.OnLoad(e);
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            StringBuilder html = new StringBuilder();
            string path = this.RootPath.Trim().EndsWith("/") ? this.RootPath.Trim() : (this.RootPath.Trim() + "/");
            string cfun = this.ClientUploadFunction.Trim();
            string errStr = "";
            if (this.RootPath.Trim() == "")
            {
                errStr = errStr + "<br><pre> 组件根目录未设置！</pre>";
            }
            if (errStr != "")
            {
                output.Write(errStr);
            }
            else
            {
                switch (this.EType)
                {
                    case EditType.Msn:
                        path = path + "msn/";
                        html.Append("<input id=\"h" + this.ID + "\" name=\"h" + this.ID + "\" type=\"hidden\" value=\"" + HttpUtility.HtmlEncode(this.Text.Trim()) + "\" />");
                        if (!this.Enable)
                        {
                            html.Append(string.Concat(new object[] { 
                                "<iframe src=\"", path, "disabled_editor.html?id=", base.ID, "&TextHeight=", this.Height.Value, "&logurl=", this.LoginUrl, "&regurl=", this.RegisterUrl, "&funstr=", cfun, "\" name=\"", base.ID, "_TempFrame\" id=\"", base.ID, 
                                "_TempFrame\" style=\"height:", this.Height, ";width:", this.Width, "\" frameborder='0' marginheight='0' marginwidth='0' scrolling='no' allowtransparency='true'></iframe>"
                             }));
                            break;
                        }
                        html.Append(string.Concat(new object[] { 
                            "<iframe style=\"border: none\" id=\"i", this.ID, "\" name=\"i", this.ID, "\" src=\"", path, "editor.html?id=h", this.ID, "&funstr=", cfun, "\" width=\"", this.Width.Value, "\" height=\"", this.Height.Value, "\" onblur=\"document.getElementById('h", this.ID, 
                            "').value=document.frames.i", this.ID, ".getHTML(true);\"></iframe>"
                         }));
                        break;

                    case EditType.Discuz:
                        path = path + "discuz/";
                        html.Append("<textarea name=\"" + base.ID + "\" id=\"" + base.ID + "\" style='display:none'>" + this.Text + "</textarea>");
                        if (!this.Enable)
                        {
                            html.Append(string.Concat(new object[] { 
                                "<iframe src=\"", path, "disabled_editor.html?id=", base.ID, "&TextHeight=", this.Height.Value, "&logurl=", this.LoginUrl, "&regurl=", this.RegisterUrl, "&funstr=", cfun, "\" name=\"", base.ID, "_TempFrame\" id=\"", base.ID, 
                                "_TempFrame\" style=\"height:", this.Height, ";width:", this.Width, "\" frameborder='0' marginheight='0' marginwidth='0' scrolling='no' allowtransparency='true'></iframe>"
                             }));
                            break;
                        }
                        html.Append(string.Concat(new object[] { "<iframe src=\"", path, "editor.html?id=", base.ID, "&TextHeight=", this.Height.Value, "\" name=\"", base.ID, "_TempFrame\" id=\"", base.ID, "_TempFrame\" style=\"height:", this.Height, ";width:", this.Width, "\" frameborder='0' marginheight='0' marginwidth='0' scrolling='no' allowtransparency='true'></iframe>" }));
                        break;
                }
                output.Write(html);
            }
        }

        [DefaultValue(""), Bindable(true), Localizable(true), Category("Appearance")]
        public string ClientUploadFunction
        {
            get
            {
                string s = (string) this.ViewState["AguiEditerClientUploadFunction"];
                return ((s == null) ? string.Empty : s);
            }
            set
            {
                this.ViewState["AguiEditerClientUploadFunction"] = value;
            }
        }

        [Category("外观"), Description("获取或设置 EditBox 控件是否可用。")]
        public bool Enable
        {
            get
            {
                object savedState = this.ViewState["Enable"];
                return ((savedState != null) && ((bool) savedState));
            }
            set
            {
                this.ViewState["Enable"] = value;
            }
        }

        [Category("输出"), Description("登录url")]
        public string LoginUrl
        {
            get
            {
                object savedState = this.ViewState["LoginUrl"];
                return ((savedState == null) ? "/ubb" : ((string) savedState));
            }
            set
            {
                this.ViewState["LoginUrl"] = value;
            }
        }

        [Category("外部的"), Description("注册url")]
        public string RegisterUrl
        {
            get
            {
                object savedState = this.ViewState["RegisterUrl"];
                return ((savedState == null) ? "/ubb" : ((string) savedState));
            }
            set
            {
                this.ViewState["RegisterUrl"] = value;
            }
        }

        [Localizable(true), DefaultValue(""), Bindable(true), Category("Appearance")]
        public string RootPath
        {
            get
            {
                string s = (string) this.ViewState["AguiEditerRootPath"];
                return ((s == null) ? string.Empty : s);
            }
            set
            {
                this.ViewState["AguiEditerRootPath"] = value;
            }
        }

        [Bindable(true), Localizable(true), Category("Appearance"), DefaultValue("")]
        public string Text
        {
            get
            {
                string s = this.GetText();
                s = (s == null) ? ((string) this.ViewState["AguiEditerText"]) : s;
                return ((s == null) ? string.Empty : s);
            }
            set
            {
                this.ViewState["AguiEditerText"] = value;
            }
        }
    }
}

